<?php

namespace Home\Controller;
use Common\Controller\HomecommController;
class CommonController extends HomecommController{

	public function index(){
		
		$count = M ( "Phone" )->where(array('Issale'=>2))->count();
		$this->assign ( "count", $count );
		$link = M ( "Link" );
		$linklist =$link->select();
		$this->assign ( "linklist", $linklist );
		$this->display ();
	}

	/**
	*	预约看车
	* issale---1表示买车，0表示卖车
	**/
	public function addPhone()
	{
		$data['Id']= create_guid();//创建id
		$data['Phone']=I("post.phone");//电话号码
		$data['PStatus']=1;
		$data['IP']=get_client_ip();//获取IP地址
		$data['Wdate']=date("Y-m-d H:i:s");
		$data['Carid'] =I("post.carid",0);//车辆id
		$data['PhoneNeed'] =I("post.phoneneed");//描述
		$issale =I("post.issale");//1表示买车，0表示卖车
		if(!$issale)$issale=1;
		$data['Issale'] =$issale;
		$m= M("Phone"); 
		$c =$m->where(array('Phone'=>$data['Phone'],'PStatus'=>1))->count();
		if($c>0)
		{
			echo json_encode(array('id'=>1,'msg'=>'请不要重复提交'));
			exit;
		}
		$ips =$m->where('IP="'.$data['IP'].'" and Wdate < "'.date('Y-m-d 00:00:00').'"')->count();
		if($ips>50)
		{
			echo json_encode(array('id'=>1,'msg'=>'操作频繁，请晚些再试'));
			exit;
		}
		$result = $m->add($data);
		if($result)
		{
			echo json_encode(array('id'=>1,'msg'=>'提交成功,请保持手机畅通'));
			exit;
		}
	}

	//获取验证码
	public function getCode()
	{
		$phone = $_POST['phone'];
		$text = '';
		$sms = new CommonController();
		$sms->sendMessage($phone,$text);
	}

	/**
	**	砍价
	**/
	public function cutPrice()
	{
		try{
			$flag = $_POST['flag'];
			$phone = $_POST['phone'];
			$phonecode = $_POST['phonecode'];
			if(empty($flag) || inject_check($flag))
				return $this->response(array("Ret" => 1,"Data" =>"", "Msg" => "验证码错误"),'json');
			if(empty($phone) || inject_check($phone))
				return $this->response(array("Ret" => 1,"Data" =>"", "Msg" => "手机号码错误"),'json');
			
			$sms = new CommonController();
			if(empty($phonecode) || inject_check($phonecode) || !$sms->checkVerify())
				return $this->response(array("Ret" => 1,"Data" =>"", "Msg" => "输入验证码错误"),'json');

			// $Model = new \Think\Model();
			// $smLogModel = M('Sms_log');
			// $rls = $smLogModel->where(array('SmsPhone'=>$phone,'SmsFlag'=>$flag))->find();
			// // $rls = $Model->query( "SELECT `smscode` FROM nc_sms WHERE SmsPhone='$phone' AND SmsFlag='$flag' Order By SmsDate Desc LIMIT 0,1;");
			// if(count($rls) < 1)
			// return $this->response(array("Ret" => 1,"Data" =>"", "Msg" => "输入验证码错误1，请检查后输入"),'json');
			// if($rls[0]["smscode"] != $phonecode)
			// return $this->response(array("Ret" => 1,"Data" =>"", "Msg" => "输入验证码错误2，请检查后输入"),'json');
			//验证码

			//查询用户是否存在
			$customerModel = M('Customer');
			$rls = $customerModel->field('Id,CusPwd')->where(array('CusPhone'=>$phone))->find();
			$customerData['Id'] = '';
			$customerData['CusPwd'] = '';
			$customerData['CusPhone'] = $phone;
			$cusid = "";
			$cuspwd = "";
			if(count($rls) < 1)
			{
				$customerData['Id'] = create_guid();
				$customerData['CusPwd'] = $phone;
				$customerModel->add($customerData);
				// $rls = $Model->execute("Insert into nc_customer(Id,CusPhone,CusPwd) Values('$cusid','$phone','$phone');");
				if(!$rls)
				{
					return $this->response(array("Ret" => 1,"Data" =>"", "Msg" => "新增用户失败！"),'json');
				}
			}
			else
			{
				$cusid = $rls[0]["id"];
				$cuspwd =$rls[0]["cuspwd"];
			}
			$carid = $_POST['carid'];
			$price = $_POST['price'];
			if(!is_numeric( $price ) || $price < 0.1 || $price > 1000)
				return $this->response(array("Ret" => 1,"Data" =>"", "Msg" => "价格错误！"),'json');
			$car = D ( "Cars" );
			$conditioncardt['Id'] = $carid;
			$carno = $car->where($conditioncardt)->count();
			if($carno < 1)
				return $this->response(array("Ret" => 1,"Data" =>"", "Msg" => "无车辆"),'json');
				
			$cutprice = D("cutprice");
			$cutpricecdt['CusId'] = $cusid;
			$cutpricecdt['CarId'] = $carid;
			$cutprice->where($cutpricecdt)->delete(); 
		
			$cutpricecdt["Id"] = create_guid();
			$cutpricecdt['CutPrice']= $price;
			$cutpricecdt['CutDate']= date('Y-m-d H:i:s',time());
			$result = $cutprice->add($cutpricecdt);
			return $this->response(array("Ret" => 0,"Data" =>array("Id"=>$cusid,"CusPwd"=>$cuspwd), "Msg" => ""),'json');
		}
		catch(Exception $e)
		{
			return $this->response(array("Ret" => 1,"Data" =>"","Msg" => $e->getMessage()),'json');
		}
		$url = "http://125.67.237.182:8000/svr/cutpricewithreg";
		$post_data = array ("carid" => $carid,"price" => $price,"flag" => $flag,"phone" => $phone,"phonecode" => $phonecode);
		//var_dump($post_data);
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
		$output = curl_exec($ch);
		curl_close($ch);
		//var_dump($output);
		echo $output;
				
	}
}
?>